package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.protocol.IDynamicCacheService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCommonDataProperty;
import com.ruoyi.system.service.ISysCommonDataPropertyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公共基础字段Controller
 * 
 * @author ruoyi
 * @date 2021-10-31
 */
@RestController
@RequestMapping("/system/commonDataProperty")
public class SysCommonDataPropertyController extends BaseController
{
    @Autowired
    private ISysCommonDataPropertyService sysCommonDataPropertyService;

    @Autowired
    private IDynamicCacheService dynamicCache;

    /**
     * 查询公共基础字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCommonDataProperty sysCommonDataProperty)
    {
        startPage();
        List<SysCommonDataProperty> list = sysCommonDataPropertyService.selectSysCommonDataPropertyList(sysCommonDataProperty);
        return getDataTable(list);
    }

    /**
     * 导出公共基础字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:export')")
    @Log(title = "公共基础字段", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCommonDataProperty sysCommonDataProperty)
    {
        List<SysCommonDataProperty> list = sysCommonDataPropertyService.selectSysCommonDataPropertyList(sysCommonDataProperty);
        ExcelUtil<SysCommonDataProperty> util = new ExcelUtil<SysCommonDataProperty>(SysCommonDataProperty.class);
        return util.exportExcel(list, "公共基础字段数据");
    }

    /**
     * 获取公共基础字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysCommonDataPropertyService.selectSysCommonDataPropertyById(id));
    }

    /**
     * 新增公共基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:add')")
    @Log(title = "公共基础字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommonDataProperty sysCommonDataProperty)
    {
        int i = sysCommonDataPropertyService.insertSysCommonDataProperty(sysCommonDataProperty);
        dynamicCache.refreshCommonDataPropertyListCache();
        return toAjax(i);
    }

    /**
     * 新增字段判重
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:add')")
    @GetMapping("commonAddRemovingRepeat/{nameEn}")
    public AjaxResult commonAddRemovingRepeat(@PathVariable("nameEn") String nameEn){
        List<SysCommonDataProperty> commonDataPropertyList = sysCommonDataPropertyService.selectSysCommonDataPropertyList(null);
        SysCommonDataProperty sysCommonDataProperty = commonDataPropertyList.stream().filter(x -> nameEn.equals(x.getNameEn())).findFirst().orElse(null);
        if(sysCommonDataProperty != null){
            return success().put("msg", "字段名不可用，已存在该公共字段");
        }
        return success().put("msg", "success");
    }

    /**
     * 修改公共基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:edit')")
    @Log(title = "公共基础字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCommonDataProperty sysCommonDataProperty)
    {
        int i = sysCommonDataPropertyService.updateSysCommonDataProperty(sysCommonDataProperty);
        dynamicCache.refreshCommonDataPropertyListCache();
        return toAjax(i);
    }

    /**
     * 删除公共基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonDataProperty:remove')")
    @Log(title = "公共基础字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = sysCommonDataPropertyService.deleteSysCommonDataPropertyByIds(ids);
        dynamicCache.refreshCommonDataPropertyListCache();
        return toAjax(i);
    }
}
